import pygame
import random

class entity(object):
    """An object that exists in the world"""
    textures = {} #Textures that all entities have access to. Store textures for entities here.

    def __init__(self, manager):
        self.rect = pygame.Rect(0,0,0,0) #The drawing dimensions for the sprite.
        self.type = "" #Type is entered into the dictionary when it's drawn to find the correct texture.
        #self.customTexture = False #Used only if the entity has a texture that is generated in the code that should replace a loaded texture
        self.color = (random.random() * 255,random.random() * 255,random.random() * 255)
        self.layer = 1 #Layer drawn on, 1 is base. 0 is in front, 1 is in behind.
        self.drawDebug = False
        self.updates = 0 # If the entity needs to update while the game is running, this should be True.
        self.collides = True

        self.attached = {}

    def moveTo(self, x, y):
        """Sets the entities position to (x,y)"""
        self.rect.topleft = (x,y)

    def moveBy(self, x, y):
        """Moves the entity horizontally by x, and vertically by y"""
        self.rect.move_ip(x,y)

    def setSize(self, width, height):
        """Sets the width and height of the entity"""
        self.rect.width = width
        self.rect.height = height
    
    def update(self, timeElapsed, manager):
        for en in self.attached.keys():
            en.rect.topleft = (self.attached[en][1] + self.rect.left, self.attached[en][2] + self.rect.top)

    def attachEntity(self, entity, xOffset):
        self.attached[entity] = [xOffset, yOffset]


    def checkCollide(self, rect):
        if(self.rect.colliderect(rect)):
            return True
        return False
        
    def onSave(self, manager, levelData):
        mySaveData = {}
        mySaveData['class'] = self.__class__
        mySaveData['rect'] = self.rect
        mySaveData['type'] = self.type
        mySaveData['layer'] = self.layer
        levelData.append(mySaveData)
        
    @staticmethod
    def onLoad(manager, saveData):
        newEntity = saveData['class'](manager)
        newEntity.rect.width = saveData['rect'].width
        newEntity.rect.height = saveData['rect'].height
        newEntity.rect.topleft = saveData['rect'].topleft
        newEntity.type = saveData['type']
        newEntity.layer = saveData['layer']
        manager.addEntity(newEntity, newEntity.update)
        return newEntity

    def getScreenPosition(self, camera, manager):
        parallax = manager.layerParallax[int(self.layer)]
        return (self.rect.topleft[0] - camera.rect.topleft[0] * parallax[0], self.rect.topleft[1] - camera.rect.topleft[1] * parallax[1])

    def draw(self, camera, screen, manager):
        """Draws to the screen in relation to the camera."""

        #camera: Camera being drawn in relation to
        #screen: Surface being drawn to
        #customTexture to drawInstead of default
        
        if(self.type != ""):        
            drawTexture = manager.textures[self.type]            
            screen.blit(drawTexture, self.getScreenPosition(camera, manager)) #Draws entity texture in entity rect to screen.
        else:
            screenPos = self.getScreenPosition(camera, manager)
            pygame.draw.rect(screen, self.color, ( screenPos[0], screenPos[1], self.rect.width, self.rect.height), 3)


